//
//  DepartmentViewController.m
//  Prospectus
//
//  Created by Ken Power on 24/01/2011.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//



#import "ProgrammeViewController.h"
#import "DataAccess.h"
#import "DepartmentViewController.h"

@implementation DepartmentViewController

@synthesize department,groupedProgrammes;


- (void)viewDidLoad {
    [super viewDidLoad];
	
	NSMutableString* dept=[NSMutableString stringWithString:NSLocalizedString(@"DepartmentOf",nil)];
	[dept appendString:self.department.Name];
	
	self.navigationController.navigationBar.frame;
	UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 400, 50)];
	self.navigationItem.titleView = label;
	// set the label view to have a clear background and a 20 point font
	label.backgroundColor = [UIColor clearColor];
	label.font = [UIFont boldSystemFontOfSize:25];
	label.textColor=[UIColor whiteColor];
	label.text=dept;
	//label.adjustsFontSizeToFitWidth=YES;  
	if ([label.text length]>10 ) {
		label.font = [UIFont boldSystemFontOfSize:15];
	}
	label.minimumFontSize=2;
	label.numberOfLines=3;
	[label layoutIfNeeded];

	label.lineBreakMode=UILineBreakModeWordWrap;


	[label release];
	
	//self.navigationItem
	
	// Set up the edit and add buttons.
    //self.navigationItem.leftBarButtonItem = self.editButtonItem;
    
	//// Configure the add button.
    //UIBarButtonItem *addButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(addBook)];
    //self.navigationItem.rightBarButtonItem = addButton;
    //[addButton release];
	
	
	NSArray* programmes=[DataAccess getProgrammesInDepartment:self.department.Abbreviation];
	if (programmes==nil) {
		NSLog(@"NO programmes found in %@",[self.department.Programmes count],self.department.Name);
		exit(-1);
	}
	NSLog(@"%i programmes found in %@",[self.department.Programmes count],self.department.Name);
	
	// group by NQF level
	NSMutableArray* groups =[[[NSMutableArray alloc]init]autorelease];
	
	for(int i=6; i<=8;i++){ //NFQLevels 6,7,& 8 form groups
		NSPredicate *predicate = [NSPredicate predicateWithFormat:
								  @"NFQLevel == %i", i];
		NSArray* array=[programmes filteredArrayUsingPredicate:predicate];
		if ([array count]>0) {
			[groups addObject:array];
		}
		NSLog(@"%i programmes added at NQFLevel:%i",[array count],i);		
		
	} 
	self.groupedProgrammes = groups;
	
}

- (void)viewWillAppear:(BOOL)animated {
    // Redisplay the data.
    [self.tableView reloadData];
	//[self updateRightBarButtonItemState];
}

- (void)viewDidUnload {
	// Release any properties that are loaded in viewDidLoad or can be recreated lazily.
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return [self.groupedProgrammes count];
}

// Customize the number of rows in the table view.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
	
	return [[self.groupedProgrammes objectAtIndex:section] count];
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
    
	
	//Programme *prog = [[groupedProgrammes objectAtIndex:section]  objectAtIndex:0];
//	switch (prog.NFQLevel.intValue) {
//		case 6:
//			title=@"Higher Certificates";
//			break;
//		case 7:
//			title=@"Ordinary Bachelor Degrees";
//			break;
//
//		case 8:
//			title=@"Honours Bachelor Degrees";
//			break;
//			
//		default:
//			break;
//	}
	
	
	NSArray* ga=[self.groupedProgrammes objectAtIndex:section];
	if(ga==nil || [ga count]==0){
		NSLog(@"No programes in section %@, quitting\n",section);
		exit(-1);
	}
	Programme *prog = [ga objectAtIndex:0];
	//Section title will be the Award type of the first(and all) programmes in each group
	NSString* title=[prog AwardType];
	
	return title;
}


// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *CellIdentifier = @"Cell";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
    }
    
    // Configure the cell.
	[self configureCell:cell atIndexPath:indexPath];
    return cell;
}

- (void)configureCell:(UITableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath {
	
	NSLog(@"configuring cell at indexpath(%d,%d)\n",[indexPath indexAtPosition:0],[indexPath indexAtPosition:1]);

	NSArray* nfqGroup=[self.groupedProgrammes objectAtIndex:[indexPath indexAtPosition:0]];
	Programme *prog = [nfqGroup objectAtIndex:[indexPath indexAtPosition:1]];

	NSLog(@"found programme: %@",prog.CAOCode);
	cell.textLabel.text = prog.Title;
	
	NSMutableString* detail=[NSMutableString stringWithString:[prog CAOCode]];
	//[detail appendString:@" "];
	//[detail appendString:prog.AwardType];
	cell.detailTextLabel.text=detail;
	cell.textLabel.font=[UIFont fontWithName:@"Verdana" size:16];
	cell.accessoryType =  UITableViewCellAccessoryDisclosureIndicator;

}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
	
    // Create and push a detail view controller.
	ProgrammeViewController *programmeViewController = 
		[[ProgrammeViewController alloc] initWithNibName:@"ProgrammeView" bundle:nil];
	
	// Pass the selected course to the new view controller.
   	NSArray* NFQgroup=[self.groupedProgrammes objectAtIndex:[indexPath indexAtPosition:0]];
	Programme *selectedProg = [NFQgroup objectAtIndex:[indexPath indexAtPosition:1]];

	programmeViewController.programme = selectedProg;
	
	[self.navigationController pushViewController:programmeViewController animated:YES];
	[programmeViewController release];
}


- (void)dealloc {
	[groupedProgrammes release];
	[department release];
    [super dealloc];
}
@end
